package driver;


import net.aiotos.driver.IOTOSDriverI;
import net.aiotos.driver.IotosEngine;
import net.aiotos.common.dto.*;
import net.aiotos.common.exception.DataNotExistError;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;


@Slf4j
public class HelloWorldDriver extends IOTOSDriverI {
    public HelloWorldDriver(@NonNull DeviceDTO deviceDTO) {
        super(deviceDTO);
    }

    @Override
    public void InitComm() throws Exception {
        ResultDTO resultDTO = getZm().DevOnline(new String[]{getZm().getUuid() + "." + getSysId()});
        if(resultDTO.getCode() != ErrorEnum.Successfull) {
            log.warn("{},deviceId={},上线失败", getClass().getName(), getSysId());
        }
        log.info("deviceId={}", getSysId());
        log.info("uuid={}", getZm().getUuid());
        List<DataPubDTO> dataPubDTOList = new ArrayList<>();
        setCollectingOneCircle(false);

        try {
            String value = this.value(name("33dd"), null, "p");
            log.info("value:{}", value);
            dataPubDTOList.add(dataPub("33dd", 1));
            dataPubDTOList.add(dataPub("a5e2", 11.333));
            log.info("setValues={}", dataPubDTOList);
            List<ResultDTO> setValues = setValues(dataPubDTOList);
            log.info("setValues={}", setValues);
        } catch (DataNotExistError error) {
            log.warn("请确认该数据点拼写是否正确:{}", error.dataId, error);
        } catch (Exception error) {
            log.warn("未知异常:{}", error);
        }
    }

    Random random = new Random();

    @Override
    public Object[] Collectig(DataDTO dataDTO) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Object value;
        DataTypeEnum dataTypeEnum = DataTypeEnum.valueOf(dataDTO.getValuetype());
        if(dataTypeEnum == DataTypeEnum.BOOL) {
            IntStream ints = random.ints(1, 0, 1);
            value = ints.toArray()[0];
        } else if(dataTypeEnum == DataTypeEnum.FLOAT) {
            value = Math.random();
            while (true) {
                DoubleStream ints = random.doubles(1, dataDTO.getMinvalue(), dataDTO.getMaxvalue());

                double[] ints1 = ints.toArray();
                if(ints1[0] != 0) {
                    value = ints1[0];
                    break;
                }
            }

        } else {
            value = System.currentTimeMillis();
        }
        log.info("dataId:{},value:{}", dataDTO.getDataOid(), value);
        return new Object[]{value};
    }

    @Override
    public ResultDTO Event_customBroadcast(String fromUuid, String type, String data) {
        return ResultDTO.code(500).msg("设备获取接口没有实现");
    }

    @Override
    public ResultDTO Event_getData(DataDTO dataDTO, String condition) {
        return ResultDTO.code(500).msg("设备获取接口没有实现");
    }

    @Override
    public ResultDTO Event_setData(DataDTO dataDTO, String value) {
        return ResultDTO.code(500).msg("设备设置接口没有实现");
    }

    @Override
    public ResultDTO Event_syncPubMsg(String point, String value) {
        return ResultDTO.code(500).msg("设备获取接口没有实现");
    }

    public static void main(String[] args) throws Exception {
        IotosEngine iotosEngine = new IotosEngine(HelloWorldDriver.class);
        iotosEngine.engineRun(args);
    }
}
